import {
  LanguageTypeArray,
  QuestionnaireType,
  QuestionProperType,
  QuestionType,
  QuestionTypeString,
} from './typing';

export const getKey = (id?: number): string => {
  const idPrefix = Math.floor(Math.random() * 1000);
  if (id) {
    return idPrefix + '-' + (id + 1);
  }
  return idPrefix.toString();
};

export const createQuestionDesignData = (
  type: QuestionTypeString,
  languages: LanguageTypeArray,
  index: number,
): QuestionType => {
  const newIndex = index + 1;
  let questionData: QuestionType = {
    id: newIndex,
    number: type !== 'paging' ? newIndex : -1,
    key: getKey(index),
    order: newIndex,
    index,
    type,
    mode: 'design',
    mustAnswer: false,
    activeDesignLanguage: languages[0],
  };

  if (type === 'single') {
    questionData = createSingleQuestionDesignData(questionData, languages);
  } else if (type === 'multiple') {
    questionData = createMultipleQuestionDesignData(questionData, languages);
  } else if (type === 'map') {
    questionData = createMapQuestionDesignData(questionData, languages);
  } else if (type === 'scale') {
    questionData = createScaleQuestionDesignData(questionData, languages);
  } else if (type === 'answer') {
    questionData = createShortAnswerQuestionDesignData(questionData, languages);
  } else if (type === 'paragraph') {
    questionData = createParagraphQuestionDesignData(questionData, languages);
  } else if (type === 'startPage') {
    questionData = createStartPageQuestionDesignData(questionData, languages);
  } else if (type === 'endPage') {
    questionData = createEndPageQuestionDesignData(questionData, languages);
  }
  return questionData;
};

export const resetQuestionDataWithLanguage = (
  questionnaire: QuestionnaireType,
): QuestionnaireType => {
  const language = questionnaire.language;
  const hasZh = language.includes('zh');
  const hasEn = language.includes('en');

  questionnaire.questions?.forEach((eachQuestion, index) => {
    if (hasZh) {
      if (!eachQuestion.questionProperZh) {
        const temporaryQuestion = createQuestionDesignData(eachQuestion.type, ['zh'], index);
        eachQuestion.questionProperZh = temporaryQuestion.questionProperZh;
        eachQuestion.activeDesignLanguage = 'zh';
      }
    } else {
      if (eachQuestion.questionProperZh) {
        eachQuestion.questionProperZh = undefined;
        eachQuestion.activeDesignLanguage = 'en';
      }
    }

    if (hasEn) {
      if (!eachQuestion.questionProperEn) {
        const temporaryQuestion = createQuestionDesignData(eachQuestion.type, ['en'], index);
        eachQuestion.questionProperEn = temporaryQuestion.questionProperEn;
      }
    } else {
      if (eachQuestion.questionProperEn) {
        eachQuestion.questionProperEn = undefined;
        eachQuestion.activeDesignLanguage = 'zh';
      }
    }
  });
  return questionnaire;
};

const getDisplayQuestionNumber = (index: number): string => {
  return String(index + 1).padStart(3, '0');
};

const createSingleQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index ?? 0);
    questionProper.title =
      eachLanguage === 'zh' ? `单选题 - ${questionNumber}` : `Single Choice - ${questionNumber}`;
    questionProper.items = [];
    for (let itemIndex = 0; itemIndex < 3; itemIndex++) {
      const itemValue = itemIndex + 1;
      questionProper.items.push({
        id: itemIndex + 1,
        key: getKey(itemIndex),
        title: eachLanguage === 'zh' ? `选项-${itemValue}` : `option - ${itemValue}`,
        value: itemValue,
        isDefault: false,
        isInput: false,
        isExclusion: false,
        disabled: false,
        designMode: 'add',
      });
    }
    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });

  return questionData;
};

const createMultipleQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index || 0);
    questionProper.title =
      eachLanguage === 'zh' ? `多选题 - ${questionNumber}` : `Multiple Choice - ${questionNumber}`;
    questionProper.items = [];
    for (let itemIndex = 0; itemIndex < 3; itemIndex++) {
      const itemValue = itemIndex + 1;
      questionProper.items.push({
        id: itemIndex + 1,
        key: getKey(itemIndex),
        title: eachLanguage === 'zh' ? `选项-${itemValue}` : `option - ${itemValue}`,
        value: itemValue,
        isDefault: false,
        isInput: false,
        isExclusion: false,
        disabled: false,
        designMode: 'add',
      });
    }
    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });

  return questionData;
};

const createMapQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index || 0);
    questionProper.title =
      eachLanguage === 'zh' ? `地图题 - ${questionNumber}` : `Map question - ${questionNumber}`;
    questionProper.mapItem = { searchKey: '' };

    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

const createScaleQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index || 0);
    questionProper.title =
      eachLanguage === 'zh' ? `量表题 - ${questionNumber}` : `Scale question - ${questionNumber}`;

    let itemId = 1;
    questionProper.items = [];
    for (let itemIndex = 0; itemIndex < 6; itemIndex++) {
      const itemValue = itemIndex + 1;
      questionProper.items.push({
        id: itemId,
        key: getKey(itemId),
        title: itemValue,
        value: itemValue,
        isDefault: false,
        isInput: false,
        isExclusion: false,
        disabled: false,
        designMode: 'add',
        isIndividual: false,
        isHovered: false,
      });
      itemId++;
    }
    questionProper.items[0].title = eachLanguage === 'zh' ? '非常不满意' : 'very dissatisfied';
    questionProper.items[questionProper.items.length - 1].title =
      eachLanguage === 'zh' ? '非常满意' : 'very satisfied';

    questionProper.items.push({
      id: itemId,
      key: getKey(itemId),
      title: eachLanguage === 'zh' ? '不适用' : 'not applicable',
      value: -1,
      isDefault: false,
      isInput: false,
      isExclusion: false,
      disabled: false,
      isIndividual: true,
      isHovered: false,
      designMode: 'add',
    });
    questionProper.hasIndividual = true;
    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

const createShortAnswerQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index || 0);
    questionProper.title =
      eachLanguage === 'zh'
        ? `简答题 - ${questionNumber}`
        : `Short answer question - ${questionNumber}`;
    questionProper.shortAnswerItem = { isWordLimit: false };

    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

const createParagraphQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    const questionNumber = getDisplayQuestionNumber(questionData.index || 0);
    questionProper.title =
      eachLanguage === 'zh'
        ? `段落说明 - ${questionNumber}`
        : `Paragraph description - ${questionNumber}`;
    questionProper.shortAnswerItem = { isWordLimit: false };

    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

const createStartPageQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    questionProper.title = eachLanguage === 'zh' ? `开始页` : `Start page`;

    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

const createEndPageQuestionDesignData = (
  questionData: QuestionType,
  languages: LanguageTypeArray,
): QuestionType => {
  languages.forEach(eachLanguage => {
    const questionProper = {} as QuestionProperType;
    questionProper.title = eachLanguage === 'zh' ? `结束页` : `End page`;

    if (eachLanguage === 'zh') {
      questionData.questionProperZh = questionProper;
    } else {
      questionData.questionProperEn = questionProper;
    }
  });
  return questionData;
};

export const resetQuestionData = (questionnaire: QuestionnaireType) => {
  let questionNumber = 1;
  questionnaire.questions?.forEach((eachQuestion, index) => {
    if (eachQuestion.type !== 'paging') {
      eachQuestion.number = questionNumber;
      questionNumber++;
    } else {
      eachQuestion.number = -1;
    }

    eachQuestion.order = index + 1;
  });
};
